//
//  CellOfDestinationSearchResult.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/1.
//  Copyright © 2016年 KX. All rights reserved.
#import "WebViewOfOtherViewController.h"
#import "SearchResultViewController.h"
#import "CityViewController.h"
#import "FreeTravelNetwork.h"
#import "FreeTravelMacro.h"
#import "CellOfDetailsPageDestinationSearchResult.h"
#import "CellOfDestinationSearchResult.h"
#import "SearchResultViewController.h"
#import "Masonry.h"
#import "FreeTravelMacro.h"
@interface CellOfDestinationSearchResult ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UICollectionView *supCollectionView;
@end
@implementation CellOfDestinationSearchResult
- (instancetype)initWithFrame:(CGRect)frame{
    self= [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
        self.imageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Unknown-1"]];
        
        [self.contentView addSubview:_imageView];
        

    }
    return self;
}
- (void)handleData{
    
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/search/index?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&keyword=%@&page=1&place_field=country|city|poi&track_app_channel=App%%20Store&track_app_version=7.0.1&track_device_info=iPhone%%205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%209.3.4&type=place&v=1",self.mosaicString] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        self.model = [ModelOfDestinationDataSearchResult modelWithDic:dic];
        if (self.model.entry.count == 0) {
            self.imageView.hidden = NO;
            
        }else{
            self.imageView.hidden = YES;
        }
        [self.collectionView reloadData];
                
        
    } andFailureBlock:^(NSError *error) {
        
    }];
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, 150);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.contentView.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[CellOfDetailsPageDestinationSearchResult class] forCellWithReuseIdentifier:@"CellOfDetailsPageDestinationSearchResult"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.model.entry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfDetailsPageDestinationSearchResult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfDetailsPageDestinationSearchResult" forIndexPath:indexPath];
    cell.model = self.model.entry[indexPath.item];
    
    return cell;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(SCREEN_WIDTH/2);
        make.height.equalTo(self.imageView.mas_width);
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    
}
- (void)setMosaicString:(NSString *)mosaicString{
    _mosaicString = mosaicString;
    

    [self handleData];
}
- (void)setModel:(ModelOfDestinationDataSearchResult *)model{
    _model = model;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ModelOfDestinationSpecificSearchResult *model = self.model.entry[indexPath.item];

    if (model.type == 2) {
        CityViewController *vc = [[CityViewController alloc]init];
        
        vc.ID = model.ID;
        vc.type = model.type;
        SearchResultViewController *searchVC = (SearchResultViewController *)self.nextResponder.nextResponder.nextResponder;
        [searchVC.navigationController pushViewController:vc animated:YES];
    }else if(model.type == 3){
        CountryViewController *vc = [[CountryViewController alloc]init];
        vc.ID = model.ID;
        vc.type = model.type;
        SearchResultViewController *searchVC = (SearchResultViewController *)self.nextResponder.nextResponder.nextResponder;
        [searchVC.navigationController pushViewController:vc animated:YES];
    }else{
        WebViewOfOtherViewController *WebView = [[WebViewOfOtherViewController alloc]init];
            
        WebView.ID = model.ID;
        WebView.type = model.type;

        
        
//        OtherViewController *vc = [[OtherViewController alloc]init];
//        vc.ID = model.ID;
//        vc.type = model.type;
        SearchResultViewController *searchVC = (SearchResultViewController *)self.nextResponder.nextResponder.nextResponder;
        [searchVC.navigationController pushViewController:WebView animated:YES];
    }
    
    
    
}
@end
